﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;
using System.Xml;
using MicroLib;

namespace MicroSipder
{
    class Program
    {
        static void AddSNSUsers(string hostid, MicroLib.Person[] followers)
        {

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            string sql = "set names utf8";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
                
            for (int i = 0; i < followers.Length; i++)
            {
                try
                {
                    
                sql = "call spAddFollowers ('"+hostid+"','0','" + followers[i].id + "', '" + followers[i].screen_name + "', '" + followers[i].province + "', '" + followers[i].city + "', '" + followers[i].location + "', '" + followers[i].description + "', '" + followers[i].url + "', '" + followers[i].profile_image_url + "', '" + followers[i].gender + "', '" + followers[i].createdTime + "')";
                cmd.CommandText = sql;
               
                cmd.ExecuteNonQuery();
                    
                }
                catch (Exception ex)
                {
                    Util.AddLog("Error when add followers. SQL=" + sql + ".\n" + ex.ToString());
                   
                }

            }
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            cmd = null;    
            conn = null;
            
        }
        static MicroLib.MicroBlogSina sina = new MicroLib.MicroBlogSina();

        static void SpiderSNS(string userid, int degree)
        {
            MicroLib.Person[] followers = null;
  
            string cursor = "-1";
            do
            {
                try
                {
                    if (Util.method == 1)
                        followers = sina.GetFollowers(userid, ref cursor);
                    else
                        followers = sina.GetFollowers2(userid, ref cursor);

                    AddSNSUsers(userid, followers);

                    if (degree > 0)
                    {
                        for (int i = 0; i < followers.Length; i++)
                        {
                            SpiderSNS(followers[i].id, degree - 1);
                        }

                    }
                }
                catch (System.Net.WebException e)
                {

                    Util.AddLog("Get WebException when spider userid=" + userid + "; degree=" + degree.ToString() + ".\n" + e.ToString());
                    sina.Logout();
                    Util.GetAvailableAccount();
                    Util.AddLog("Try to login with another account:" + Util.user);
                    sina.Login(Util.user, Util.password, "");
                }
                catch (Exception e)
                {
                    Util.AddLog("Error in main.\n" + e.ToString());
                }
            } while (cursor != "0"&&cursor !="20");
        }
       
        static void Main(string[] args)
        {
            MicroLib.Person[] followers = null;

            try
            {
                
                Util.InitConfig();
                Util.GetAvailableAccount();
                sina.Login(Util.user, Util.password, Util.cookie);
                
                if (Util.startid != "-1")
                {
                    followers = sina.GetUserDetails(Util.startid);
                    AddSNSUsers("-1", followers);
                }
                else
                {
                    Util.startid = GetLatestUser();
                }
                SpiderSNS(Util.startid, Util.degree);
            }
            catch (Exception e)
            {
                Util.AddLog("Error in main.\n" + e.ToString());
                
            }
        }
        static string GetLatestUser()
        {
            string userid;

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            string sql = "set names utf8";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.ExecuteNonQuery();

            cmd.CommandText = "select * from snsusers order by idsnsusers DESC limit 1";
            MySqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            userid = (string)dr["userid"];
            return userid;
        }
    }
}
